<template>
  <Dialog v-model="dialogVisible" width="600px" :title="`${crudInfo ? '编辑' : '添加'}反向代理`" maxHeight="400px"
    @close="closeHandle">
    <el-form ref="ruleFormRef" :model="formData" :rules="formRules" label-width="110px" class="w-[500px]">
      <el-form-item label="代理名称" prop="proxyName" required>
        <el-input v-model="formData.proxyName" type="text" placeholder="请输入" clearable show-word-limit
          maxlength="220" />
      </el-form-item>
      <el-form-item label="目标URL" prop="targetURL" required>
        <el-input v-model="formData.targetURL" type="text" placeholder="请输入" clearable show-word-limit
          maxlength="220" @input="targetURLChange"/>
      </el-form-item>
      <el-form-item label="发送域名" prop="sendDomainName" required>
        <el-input v-model="formData.sendDomainName" type="text" placeholder="请输入" clearable show-word-limit
          maxlength="220" />
      </el-form-item>
      <el-form-item label="缓存时间(分)" prop="cacheTime" required>
        <el-input v-model.number="formData.cacheTime" type="number" placeholder="请输入" clearable show-word-limit
          maxlength="220" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeHandle">关闭</el-button>
      <el-button type="primary" :loading="loading" @click="saveHandle(ruleFormRef)">提交</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { ref, computed, watch, defineEmits, reactive, onMounted } from 'vue'
import { proxyManagePhpWebAPI, listWebDomainsAPI } from "@/api/systemMonitor/site"
import { propTypes } from '@/utils/propTypes'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'

const emit = defineEmits(['update:modelValue', 'confirm'])

const props = defineProps({
  modelValue: propTypes.bool.def(false),
  info: propTypes.object.def(undefined),
  webInfo: propTypes.object.def(undefined),
  crudInfo: propTypes.object.def(undefined),
})
//默认表单
const defaultFormData = {
  proxyName: '',
  sendDomainName: "",
  targetURL: "http://",
  cacheTime: 0,
}

const loading = ref(false)

let formData = reactive({
  ...defaultFormData
})

const ruleFormRef = ref<FormInstance>()
const formRules = reactive<FormRules>({
  proxyName: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  targetURL: [
    { required: true, message: '此项必填', trigger: 'blur' },
    {
      type: 'url',
      message: '请输入合法的url地址',
      trigger: ['blur'],
    },
  ],
  sendDomainName: [
    { required: true, message: '此项必填', trigger: 'blur' },
  ],
  cacheTime: [
    { required: true, message: '此项必填', trigger: 'blur' },
    {
      type: 'number',
      message: '请输入合法的时间',
      trigger: ['blur'],
    },
  ],
})
//重定向域名下拉列表
const domainNameOption = ref(null)
onMounted(() => {
})



//弹出框状态
const dialogVisible = ref(props.modelValue)

watch(() => props.modelValue, (newVal) => {
  dialogVisible.value = newVal;
  if (newVal) {
    listWebDomainsAPI({ proxyName: props.info.proxyName }).then((res) => {
      domainNameOption.value = res.data.map((item) => {
        return {
          label: item.proxyName,
          value: item.proxyName,
        }
      })
    })
    if (props.crudInfo) {
      formData = {
        ...props.crudInfo
      }
    }
  }
})

//提交
const saveHandle = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      loading.value = true;
      proxyManagePhpWebAPI({
        ...formData,
        domainName:props.info.domainName
      }).then(() => {
        ElMessage.success("添加成功")
        closeHandle()
        emit('confirm')
      }).finally(() => {
        loading.value = false;
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}
//关闭
const closeHandle = () => {
  dialogVisible.value = false;
  //初始化数据
  Object.assign(formData, defaultFormData);
  emit('update:modelValue', dialogVisible)
}

//目标URL change事件
const targetURLChange = (val) => {
  formData.sendDomainName = val.replace("http://","").replace("https://","");
}

</script>

